package com.tich.blog.controller;


import com.tich.blog.domain.dto.BlogCommentDTO;
import com.tich.blog.domain.dto.BlogSonCommentDTO;
import com.tich.blog.domain.dto.CommentPublishDTO;
import com.tich.blog.domain.entity.BlogComments;
import com.tich.blog.domain.vo.CommentSimpleVO;
import com.tich.blog.service.IBlogCommentsService;
import com.tich.constant.ResponseMessageConstant;
import com.tich.result.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;

/**
 * <p>
 * 博客评论表 前端控制器
 * </p>
 *
 * @author 王志宇
 * @since 2025-05-14
 */
@Slf4j
@RestController
@RequestMapping("/comments")
public class BlogCommentsController {

    @Autowired
    IBlogCommentsService blogCommentsService;


    /**
     * 发表评论
     * @param commentPublishDTO
     * @return
     */
    @PostMapping
    public Result saveBlogComment(@RequestBody CommentPublishDTO commentPublishDTO){
        blogCommentsService.saveBlogComment(commentPublishDTO);
        return Result.success(ResponseMessageConstant.SAVE_SUCCESS);
    }

    /**
     * 删除评论
     * @param commentId
     * @return
     */
    @DeleteMapping("/comment")
    public Result deleteBlogComment(@RequestParam("id") Long commentId){
        blogCommentsService.deleteCommentById(commentId);
        return Result.success(ResponseMessageConstant.DELETE_SUCCESS);
    }


    /**
     * 获取帖子主评论
     * @param
     * @return
     */
    @GetMapping("/main")
    public Result<CommentSimpleVO> getBlogComments(@RequestParam("blogId") Long blogId, @RequestParam("cursor") LocalDateTime cursor){
        BlogCommentDTO blogCommentDTO = new BlogCommentDTO();
        blogCommentDTO.setBlogId(blogId);
        blogCommentDTO.setCursor(cursor);
        CommentSimpleVO commentSimpleVO = blogCommentsService.queryComments(blogCommentDTO);
        return Result.success(commentSimpleVO);
    }

    /**
     * 获取帖子评论的子评论
     * @param
     * @return
     */
    @GetMapping("/son")
    public Result<CommentSimpleVO> getBlogSonComments(@RequestParam("id") Long id, @RequestParam("cursor") LocalDateTime cursor){
        BlogSonCommentDTO blogSonCommentDTO = new BlogSonCommentDTO();
        blogSonCommentDTO.setParentID(id);
        blogSonCommentDTO.setCursor(cursor);
        return Result.success( blogCommentsService.getBlogSonComments(blogSonCommentDTO));
    }

    /**
     * 根据帖子id删除帖子相关的评论
     * @param blogId
     * @return
     */
    @DeleteMapping("/comments")
    public Result deleteCommentByBlogId(@PathVariable Long blogId) {
        log.info("deleteCommentByBlogId: {}", blogId);
        blogCommentsService.deleteCommentByBlogId(blogId);
        return Result.success();
    }
}
